<?php
//user验证其
namespace app\api\validate;

//TP验证模块
use think\Validate;

class User extends Validate
{
   
    //定义验证规则
    protected $rule = [
        'username'  =>  'require|length:3,12|unique:user',
        'password'    =>  'require|length:6,32',
        'email'       =>  'require|email|length:4,42|unique:user'
    ];
    
    //定义错误信息
    protected $message = [
        'username.require' => '用户名不得为空',
        'username.length' => '用户名长度不在3-12范围之内',
        'username.unique' => '用户名已存在',

        'password.repassword' => '两次输入的密码不同',
        'password.require' => '密码不得为空',
        'password.length' => '密码长度不在6-32范围之内',

        'emai.require' => '邮箱不得为空',
        'emai.length' => '邮箱长度不在4-32范围之内',
        'emai.unique' => '邮箱已存在',
        'emai.email' => '邮箱不合法'
    ];

    //验证场景-注册
    protected function sceneRegister()
    {
    	return $this->append('password', 'repassword');
    }

    //验证场景-登入
    protected function sceneLogin()
    {
    	return $this->only(['email', 'password'])
                    ->remove('email', 'unique');
    }

    //验证场景-修改密码
    protected function scenePassword()
    {
    	return $this->only(['password'])->append('password', 'repassword');
    }

    //自定义验证-两次密码是否重复
    protected function repassword($value, $rule, $data) { 
        return $data['rpassword'] === $value ? true : false;
    }

}
